﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using EnetaMvc.ApplicationServices;
using EnetaMvc.ApplicationServices.Dto;
using EnetaMvc.Web.Attributes;

namespace EnetaMvc.Web.Controllers
{
    public class DynamicMenuMngController : BaseController
    {
        private readonly IContentService _contentService;

        public DynamicMenuMngController(IContentService contentService)
        {
            _contentService = contentService;
        }

        [EnetaAuthorize(Roles = "Administrator")]
        public ActionResult Index()
        {
            return View(_contentService.ListDynamicMenus());
        }

        [EnetaAuthorize(Roles = "Administrator")]
        public ActionResult Create(long? parentId)
        {
            DynamicMenuDto menuItem = new DynamicMenuDto();
            if (parentId != null)
                menuItem.Parent = new DynamicMenuDto() { Id = parentId.Value };
            return View(menuItem);
        }

        [EnetaAuthorize(Roles = "Administrator")]
        [HttpPost]
        public ActionResult Create(DynamicMenuDto model)
        {
            _contentService.SaveDynamicMenu(model);
            return RedirectToAction("Index");
        }

        [EnetaAuthorize(Roles = "Administrator")]
        public ActionResult Edit(long id)
        {
            return View(_contentService.GetDynamicMenu(id));
        }

        [EnetaAuthorize(Roles = "Administrator")]
        [HttpPost]
        public ActionResult Edit(DynamicMenuDto model)
        {
            _contentService.SaveDynamicMenu(model);
            return RedirectToAction("Index");
        }

        [EnetaAuthorize(Roles = "Administrator")]
        public ActionResult Delete(long id)
        {
            _contentService.DeleteDynamicMenu(id);
            return RedirectToAction("Index");
        }


        public PartialViewResult LeftMenu()
        {
            string currentController = Request.RequestContext.RouteData.Values["controller"].ToString();
            string[] controllerNamesForBlogsShowing = new string[] { "news", "blogs", "blogposts", "articles" };
            
            IList<DynamicMenuDto> model = _contentService.ListDynamicMenus() ?? new List<DynamicMenuDto>();
            foreach (string str in controllerNamesForBlogsShowing)
                if (currentController.Equals(str, StringComparison.InvariantCultureIgnoreCase))
                {
                    var newsItem = model.Where(x => x.Url.StartsWith("~/news")).FirstOrDefault();
                    if (newsItem != null)
                        model = newsItem.Children;
                }
            return PartialView("_LeftMenu", model);
        }

        public PartialViewResult TopMenu()
        {
            var model = _contentService.ListDynamicMenus() ?? new List<DynamicMenuDto>();
            return PartialView("_TopMenu", model);
        }
    }
}
